//
//  HLRtcCallStatusCalledImpl.m
//  YueYue
//
//  Created by hulu001 on 2024/1/9.
//

#import "HLRtcCallStatusCalledImpl.h"
#import <NERtcSDK/NERtcSDK.h>
#import <NIMSDK/NIMSDK.h>
#import "HLRtcCallKitContext.h"
#import "HLRtcCallkit+Private.h"
#import "HLRtcCallKitConsts.h"

@implementation HLRtcCallStatusCalledImpl


@synthesize context;

- (HLRtcCallStatus)callStatus {
    return HLRtcCallStatusCalled;
}

- (void)createChannel:(HLRtcCallType)callType completion:(nullable void (^)(NSString * _Nullable, NSError * _Nullable))completion
{
    if (!completion) return;
    
    NSError *error = [NSError errorWithDomain:kHLRtcCallKitErrorDomain code:21000 userInfo:@{NSLocalizedDescriptionKey: @"正在被呼叫，不能创建频道"}];
    completion(nil, error);
}

- (void)JoinChannel:(NSString *)channelID completion:(void (^)(NSError * _Nullable))completion
{
    if (!completion) return;
    
    NSError *error = [NSError errorWithDomain:kHLRtcCallKitErrorDomain code:21001 userInfo:@{NSLocalizedDescriptionKey: @"正在被呼叫，不能加入频道"}];
    completion(error);
}

- (void)call:(NSString *)userID
        type:(HLRtcCallType)type
  completion:(void (^)(NSError * _Nullable))completion {
    if (!completion) return;
    
    NSError *error = [NSError errorWithDomain:kHLRtcCallKitErrorDomain code:20003 userInfo:@{NSLocalizedDescriptionKey: @"正在被呼叫，不能发起呼叫"}];
    completion(error);
}

- (void)groupCall:(NSArray<NSString *> *)userIDs groupID:(NSString *)groupID type:(HLRtcCallType)type completion:(void (^)(NSError * _Nullable))completion {
    if (!completion) return;
    
    NSError *error = [NSError errorWithDomain:kHLRtcCallKitErrorDomain code:20003 userInfo:@{NSLocalizedDescriptionKey: @"正在被呼叫，不能发起呼叫"}];
    completion(error);
}

- (void)hangup:(void (^)(NSError * _Nullable))completion {
    if (!completion) return;
    
    NSError *error = [NSError errorWithDomain:kHLRtcCallKitErrorDomain code:20007 userInfo:@{NSLocalizedDescriptionKey: @"正在被呼叫，不能挂断"}];
    completion(error);
}

- (void)leave:(void (^)(NSError * _Nullable))completion {
    if (!completion) return;
    
    NSError *error = [NSError errorWithDomain:kHLRtcCallKitErrorDomain code:20011 userInfo:@{NSLocalizedDescriptionKey: @"未进行通话，不能离开"}];
    completion(error);
}

- (void)cancel:(void (^)(NSError * _Nullable))completion {
    if (!completion) return;
    
    NSError *error = [NSError errorWithDomain:kHLRtcCallKitErrorDomain code:20015 userInfo:@{NSLocalizedDescriptionKey: @"未发起通话，不能取消"}];
    completion(error);
}

- (void)accept:(void (^)(NSError * _Nullable))completion {
    [[HLRtcCallKit sharedInstance] cancelTimeout];
    self.context.remoteUserID = self.context.inviteInfo.fromAccountId;
    // 信令accept的请求对象
    NIMSignalingAcceptRequest *accept = [[NIMSignalingAcceptRequest alloc] init];
    NSLog(@"%s channleId1 = %@",__func__, self.context.inviteInfo.channelInfo.channelId);
    accept.channelId = self.context.inviteInfo.channelInfo.channelId;
    accept.accountId = self.context.inviteInfo.fromAccountId;
    accept.requestId = self.context.inviteInfo.requestId;
    accept.autoJoin = YES;
    // 构建CustomInfo
//    NSData *JSONData = [NSJSONSerialization dataWithJSONObject:@{@"callType": @0, @"roomType" : self.context.roomType ,@"roomSource" : self.context.videoType} options:NSJSONWritingFragmentsAllowed error:NULL];
//    NSString *JSONString = [[NSString alloc] initWithData:JSONData encoding:NSUTF8StringEncoding];
//    accept.acceptCustomInfo = JSONString;
    // 信令：accept
    [[[NIMSDK sharedSDK] signalManager] signalingAccept:accept completion:^(NSError * _Nullable error, NIMSignalingChannelDetailedInfo * _Nullable response) {
        NSLog(@"%s channleId2 = %@",__func__, response.channelId);
        if (error) {
            [HLRtcCallKit sharedInstance].callStatus = HLRtcCallStatusIdle;
            if (completion) {
                completion(error);
            }
            return;
        }
        NSLog(@"****接收邀请error：%@response：%@",error,response);
        self.context.channelInfo = response;
        [HLRtcCallKit sharedInstance].callStatus = HLRtcCallStatusInCall;
        [HLRtcCallKit sharedInstance].channelId = response.channelId;
        if (self.context.isGroupCall) {
            // groupCall下，accept后直接进入rtc房间
            NSString *channelID = response.channelId;
            uint64_t myUid = self.context.localUid;
            [[HLRtcCallKit sharedInstance] joinRtcChannel:channelID myUid:myUid completion:^(NSError * _Nullable outError) {
                if (outError) {
                    // 如果RTC加入失败则退出
                    [[HLRtcCallKit sharedInstance] leave:^(NSError * _Nullable error) {
                        if (completion) {
                            completion(outError);
                        }
                    }];
                    return;
                } else {
                    if (completion) {
                        completion(nil);
                    }
                }
            }];
        } else {
            
            NSString *channelID = self.context.channelInfo.channelId;
//                channelID = [channelID stringByAppendingString:@"_0_0"];
            [[HLRtcCallKit sharedInstance] joinRtcChannel:channelID myUid:self.context.localUid completion:^(NSError * _Nullable error) {
                if (error) {
                    [[HLRtcCallKit sharedInstance] closeSignalChannel:^{
                        [[HLRtcCallKit sharedInstance].delegateProxy onError:error];
                        [[HLRtcCallKit sharedInstance].delegateProxy onCallEnd];
                    }];
                    return;
                }
                
                // 1to1下，接受成功还需要等待信令cid=1握手
                if (completion) {
                    completion(nil);
                }
            }];
        }
    }];
}

- (void)reject:(void (^)(NSError * _Nullable))completion {
    [[HLRtcCallKit sharedInstance] cancelTimeout];
    NIMSignalingRejectRequest *rejectRequest = [[NIMSignalingRejectRequest alloc] init];
    rejectRequest.channelId = self.context.inviteInfo.channelInfo.channelId;
    rejectRequest.accountId = self.context.inviteInfo.fromAccountId;
    rejectRequest.requestId = self.context.inviteInfo.requestId;
    [[[NIMSDK sharedSDK] signalManager] signalingReject:rejectRequest completion:^(NSError * _Nullable error) {
        [[HLRtcCallKit sharedInstance] send1to1CallRecord:NIMRtcCallStatusRejected];
        [HLRtcCallKit sharedInstance].callStatus = HLRtcCallStatusIdle;
        if (completion) {
            completion(error);
        }
    }];
}

- (void)switchCallType:(HLRtcCallType)type completion:(void (^)(NSError * _Nullable))completion
{
    if (!completion) return;
    
    NSError *error = [NSError errorWithDomain:kHLRtcCallKitErrorDomain code:20027 userInfo:@{NSLocalizedDescriptionKey: @"只能在呼叫过程中切换"}];
    completion(error);
}

@end
